package com.bootdo.order.listener;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.common.config.Constant;
import com.bootdo.common.redis.lock.CommonRedisLock;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.mq.RabbitUtil;
import com.bootdo.order.dao.OrderDao;
import com.bootdo.order.dao.OrderPayDao;
import com.bootdo.order.domain.OrderDO;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.quota.dao.QuotaDao;
import com.bootdo.quota.domain.QuotaDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @Desc 订单监听
 * @date 2020/1/16
 *
 */
@Configuration
@EnableScheduling
public class SpringFinishedListener{

    private static final Logger logger = LoggerFactory.getLogger(SpringFinishedListener.class);

    @Scheduled(cron = "0 */1 * * * ?")
    private void configureTasks() {
        OrderDao orderMapper = ApplicationContextRegister.getBean(OrderDao.class);
        OrderPayDao orderPayMapper = ApplicationContextRegister.getBean(OrderPayDao.class);
        QuotaDao quotaMapper = ApplicationContextRegister.getBean(QuotaDao.class);
        RabbitUtil rabbitUtil = ApplicationContextRegister.getBean(RabbitUtil.class);
        StringRedisTemplate stringRedisTemplate = ApplicationContextRegister.getBean(StringRedisTemplate.class);
        CommonRedisLock commonRedisLock = new CommonRedisLock(stringRedisTemplate);
        List<OrderDO> list = orderMapper.getOrderByNo();
        for (OrderDO order : list){
            //将自动回分的订单加入redis
            if (commonRedisLock.searchListByValue(Constant.ORDER, order.getId())){
                logger.debug("===========当前订单正在回分===========>" + order.getId());
                continue;
            }
            commonRedisLock.lLeftPush(Constant.ORDER, String.valueOf(order.getId()));
            //为收款户和收款户代理恢复额度
            OrderPayDO orderPay = orderPayMapper.getByOrderId(order.getId());
            JSONObject object = new JSONObject();
            object.put("date", DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
            object.put("userId", orderPay.getPayeeId());
            object.put("money", orderPay.getOrderMoney());
            object.put("orderId", orderPay.getOrderId());
            object.put("reason", "订单超时未支付");
            object.put("type", 1);
            rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());

            //恢复收款户代理额度
            QuotaDO quota = quotaMapper.get(orderPay.getPayeeAgentId());
            if (quota != null) {
                object.put("userId", orderPay.getPayeeAgentId());
                rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());
            }
            OrderDO orders = new OrderDO();
            orders.setId(order.getId());
            orders.setOvertimeStatus(2);
            orderMapper.update(orders);
            logger.info("<=========订单更新完成==========>" + orders.getId());
            commonRedisLock.lRemove(Constant.ORDER, 0, orderPay.getOrderId());
        }
    }
}
